﻿using DocumentFormat.OpenXml.InkML;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.AuthorizationUserManager.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business.Reports;
using NP.BPMReportPlatform.MaterialMasterData.Dto;
using NP.BPMReportPlatform.Reports.CommonApprovalProcessReport;
using NP.BPMReportPlatform.Reports.CommonApprovalProcessReport.Dtos;
using NP.BPMReportPlatform.Reports.ThirdFlow;
using NP.BPMReportPlatform.Reports.ThirdFlow.Dtos;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Reports.CommonApprovalProcessReport
{
    /// <summary>
    /// 通用审批报表
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/CommonApprovalProcess")]
    public class CommonApprovalProcessController : BPMReportPlatformController
    {
        private readonly ICommonApprovalProcessService _service;
        private readonly IHttpContextAccessor _httpAccessor;

        public CommonApprovalProcessController(ICommonApprovalProcessService service, IHttpContextAccessor httpAccessor)
        {
            _service = service;
            _httpAccessor = httpAccessor;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetAllAsync")]
        public async Task<PagedResultDto<GetCommonApprovalProcessOutput>> GetAllAsync([FromBody] GetCommonApprovalProcessInput input)
        {
            return await _service.GetAllAsync(input);
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ExportAsync")]
        public async Task<FileContentResult> ExportAllAsync([FromBody] GetCommonApprovalProcessInput input)
        {
            var data = await _service.ExportAsync(input);
            var ms = new MemoryStream();
            data.Write(ms);
            data.Close();
            return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }

        /// <summary>
        /// 获取报表配置数据
        /// </summary>
        /// <param name="btid"></param>
        /// <returns></returns>
        [HttpGet("GetConfigs")]
        public async Task<GetConfigsOutput> GetConfigs([FromQuery] string btid)
        {
            return await _service.GetConfigs(btid);
        }

        #region 配置页面接口

        /// <summary>
        /// 修改流程报表配置
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddOrUpdateConfig")]
        public async Task AddOrUpdateConfig([FromBody] CommonApprovalProcessConfig config)
        {
            await _service.AddOrUpdateConfig(config);
        }

        /// <summary>
        /// 获取配置数据-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetConfigPageList")]
        public async Task<PagedResultDto<CommonApprovalProcessConfig>> GetConfigPageList([FromBody] CommonApprovalProcessConfigInput config)
        {
            return await _service.GetConfigPageList(config);
        }
        
        /// <summary>
        /// 删除配置数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("DeleteConfig")]
        public async Task DeleteConfig([FromQuery] Guid id)
        {
            await _service.DeleteConfig(id);
        }

        #endregion

        #region 流程主表

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetHeaderAllAsync")]
        public async Task<PagedResultDto<CommonApprovalProcessConfigHeaderDto>> GetHeaderAllAsync([FromBody] CommonApprovalProcessConfigHeaderInput input)
        {
            return await _service.GetHeaderAllAsync(input);
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ExportHeaderExcel")]
        public async Task<XlsxFileResult<CommonApprovalProcessConfigHeaderDto>> ExportAllAsync([FromBody] CommonApprovalProcessConfigHeaderInput input)
        {
            var rst = await _service.ExportHeaderExcel(input);
            return new XlsxFileResult<CommonApprovalProcessConfigHeaderDto>(rst, "导出");
        }

        [HttpPost("HeaderAddOrUpdateConfig")]
        public async Task HeaderAddOrUpdateConfig([FromBody] CommonApprovalProcessConfigHeader header)
        {
            await _service.HeaderAddOrUpdateConfig(header);
        }

        [HttpPost("DeleteHeader")]
        public async Task DeleteHeader([FromQuery] string BTID)
        {
            await _service.DeleteHeader(BTID);
        }

        #endregion

        #region 子表配置

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetSubDataAllAsync")]
        public async Task<PagedResultDto<CommonApprovalProcessSubDataConfig>> GetSubDataAllAsync([FromBody] CommonApprovalProcessConfigHeaderInput input)
        {
            return await _service.GetSubDataAllAsync(input);
        }

        [HttpPost("AddOrUpdateSubData")]
        public async Task AddOrUpdateSubData([FromBody] CommonApprovalProcessSubDataConfig header)
        {
            await _service.AddOrUpdateSubData(header);
        }

        [HttpPost("DeleteSubData")]
        public async Task DeleteSubData([FromQuery] Guid Id)
        {
            await _service.DeleteSubData(Id);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetSubDataDetailAllAsync")]
        public async Task<PagedResultDto<CommonApprovalProcessSubDataDetailConfig>> GetSubDataDetailAllAsync([FromBody] CommonApprovalProcessConfigHeaderInput input)
        {
            return await _service.GetSubDataDetailAllAsync(input);
        }

        [HttpPost("AddOrUpdateSubDataDetail")]
        public async Task AddOrUpdateSubDataDetail([FromBody] CommonApprovalProcessSubDataDetailConfig config)
        {
            await _service.AddOrUpdateSubDataDetail(config);
        }

        [HttpPost("DeleteSubDataDetail")]
        public async Task DeleteSubDataDetail([FromQuery] Guid Id)
        {
            await _service.DeleteSubDataDetail(Id);
        }

        /// <summary>
        /// 获取BTID所有子表配置
        /// </summary>
        /// <param name="BTID"></param>
        /// <returns></returns>
        [HttpPost("GetSubDataConfigs")]
        public async Task<List<CommonApprovalProcessSubDataConfig>> GetSubDataConfigs([FromQuery] string BTID)
        {
            return await _service.GetSubDataConfigs(BTID);
        }

        #endregion

        /// <summary>
        /// 获取子表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("SearchSubDataList")]
        public async Task<object> SearchSubDataList([FromBody] GetCommonApprovalProcessInput input)
        {
            input.Authorization = _httpAccessor.HttpContext.Request.Headers["Authorization"];
            return await _service.SearchSubDataList(input);
        }

        /// <summary>
        /// 导出子表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportSubDataList")]
        public async Task<FileResult> ExportSubDataList(GetCommonApprovalProcessInput input)
        {
            input.Authorization = _httpAccessor.HttpContext.Request.Headers["Authorization"];
            var result = await _service.ExportSubDataList(input);
            return File(result.ToArray(), "application/octet-stream", $"{Guid.NewGuid()}.xlsx");
        }
    }
}
